package com.jyu.domain;

import com.jyu.game.GameJFrame;
import com.jyu.util.Cell;
import com.jyu.util.RotateStatus;

import java.util.Random;

public class Tetris {
    public Cell[] cells = new Cell[4];
    public RotateStatus[] rotateStatuses = new RotateStatus[4];
    public int count = 10000;

    public Tetris() {
    }

    public void rotateRight() {
        count++;
        Cell cell = cells[0];
        int col = cell.getCol();
        int row = cell.getRow();
        RotateStatus state = rotateStatuses[count % rotateStatuses.length];
        cells[1].setRow(row + state.getRow1());
        cells[2].setRow(row + state.getRow2());
        cells[3].setRow(row + state.getRow3());

        cells[1].setCol(col + state.getCol1());
        cells[2].setCol(col + state.getCol2());
        cells[3].setCol(col + state.getCol3());

    }

    public void rotateLeft() {
        count--;
        Cell cell = cells[0];
        int col = cell.getCol();
        int row = cell.getRow();
        RotateStatus state = rotateStatuses[count % rotateStatuses.length];
        cells[1].setRow(row + state.getRow1());
        cells[2].setRow(row + state.getRow2());
        cells[3].setRow(row + state.getRow3());

        cells[1].setCol(col + state.getCol1());
        cells[2].setCol(col + state.getCol2());
        cells[3].setCol(col + state.getCol3());

    }

    public Tetris(Cell[] cells) {
        this.cells = cells;
    }

    public Tetris(Cell[] cells, RotateStatus[] rotateStatuses) {
        this.cells = cells;
        this.rotateStatuses = rotateStatuses;
    }


    public void moveRight() {
        for (Cell cell : cells) {
            cell.right();
        }
    }

    public void moveLeft() {
        for (Cell cell : cells) {
            cell.left();
        }
    }

    public void moveDown() {
        for (Cell cell : cells) {
            cell.down();
        }
    }

    public static Tetris randomTetris() {
        Random r = new Random();
        Tetris tetris = null;
        int num = r.nextInt(7);
        switch (num) {
            case 0 -> tetris = new I();
            case 1 -> tetris = new J();
            case 2 -> tetris = new L();
            case 3 -> tetris = new O();
            case 4 -> tetris = new S();
            case 5 -> tetris = new T();
            case 6 -> tetris = new Z();
        }

        return tetris;
    }

    /**
     * 获取
     *
     * @return cells
     */
    public Cell[] getCells() {
        return cells;
    }

    /**
     * 设置
     *
     * @param cells
     */
    public void setCells(Cell[] cells) {
        this.cells = cells;
    }

    public String toString() {
        return "Tetris{cells = " + cells + "}";
    }

    /**
     * 获取
     *
     * @return rotateStatuses
     */
    public RotateStatus[] getRotateStatuses() {
        return rotateStatuses;
    }

    /**
     * 设置
     *
     * @param rotateStatuses
     */
    public void setRotateStatuses(RotateStatus[] rotateStatuses) {
        this.rotateStatuses = rotateStatuses;
    }
}
